#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/time.h>
#include <GL/glut.h>
#include "uvc_sdk.h"
/*CAM1 CFG*/
#define CAM1_PIXEL_WIDTH    (640)
#define CAM1_PIXEL_HEIGHT   (480)
#define CAM1_PIXEL_CHANNEL  (3)
#define CAM1_PIXEL_NUM      (CAM1_PIXEL_WIDTH*CAM1_PIXEL_HEIGHT)
#define CAM1_PID            (0x2bdf)
#define CAM1_VID            (0x028a)
#define CAM1_FPS            (30)
/*CAM2 CFG*/
#define CAM2_PIXEL_WIDTH    (640)
#define CAM2_PIXEL_HEIGHT   (480)
#define CAM2_PIXEL_CHANNEL  (3)
#define CAM2_PIXEL_NUM      (CAM2_PIXEL_WIDTH*CAM2_PIXEL_HEIGHT)
#define CAM2_PID            (0x2bdf)
#define CAM2_VID            (0x028a)
#define CAM2_FPS            (30)
GLubyte pixel_array1[CAM1_PIXEL_NUM*CAM1_PIXEL_CHANNEL];
GLubyte pixel_array2[CAM2_PIXEL_NUM*CAM2_PIXEL_CHANNEL];
int glWindow1,glWindow2;
int render_start=1,render_start2=1;
void idle_global()
{ 
  {
    glutSetWindow(glWindow1);
    render_start=0;
    glClear(GL_COLOR_BUFFER_BIT);
    glRasterPos2i(-1,1);
    glPixelZoom(1.0f,-1.0f);
    glDrawPixels(CAM1_PIXEL_WIDTH,CAM1_PIXEL_HEIGHT,GL_RGB,GL_UNSIGNED_BYTE,pixel_array1);
    render_start=1;
    // glutSwapBuffers();
  }
  // {
  //   glutSetWindow(glWindow2);
  //   render_start2=0;
  //   glClear(GL_COLOR_BUFFER_BIT);
  //   glRasterPos2i(-1,1);
  //   glPixelZoom(1.0f,-1.0f);
  //   glDrawPixels(CAM2_PIXEL_WIDTH,CAM2_PIXEL_HEIGHT,GL_RGB,GL_UNSIGNED_BYTE,pixel_array2);
  //   render_start2=1;
  // }
  glFlush();
  glutSwapBuffers();
  glutPostRedisplay();
}
void cam1_callback(uvc_frame_t *frame, void *ptr)
{
    struct timeval ts_cur;
    gettimeofday(&ts_cur,NULL);
    printf("[cam1_callback]: %lf ms ",(double)(ts_cur.tv_sec*100000+ts_cur.tv_usec)/1000);
    log_uvc_frame_format(frame->frame_format);
    uint8_t * data_p = (uint8_t *)(frame->data);
    while(render_start==0)
    {
      usleep(1);
    }
    cvt_yuyv2rgb(data_p,pixel_array1,640,480);
    
}
void cam2_callback(uvc_frame_t *frame, void *ptr)
{
    struct timeval ts_cur;
    gettimeofday(&ts_cur,NULL);
    printf("[cam2_callback]: %lf ms ",(double)(ts_cur.tv_sec*100000+ts_cur.tv_usec)/1000);
    log_uvc_frame_format(frame->frame_format);
    uint8_t * data_p = (uint8_t *)(frame->data);
    while(render_start2==0)
    {
      usleep(1);
    }
    cvt_yuyv2rgb(data_p,pixel_array2,CAM2_PIXEL_WIDTH,CAM2_PIXEL_HEIGHT);
}
int main(int argc , char * argv[])
{
    UVC_UTILS_DEVICE cam1;
    cam1.format      = UVC_FRAME_FORMAT_YUYV;
    cam1.frameWidth  = 640;
    cam1.frameHeight = 480;
    cam1.targetFps   = 30;
    cam1.pid         = 0x2bdf;
    cam1.vid         = 0x028a;
    cam1.cb          = cam1_callback;
    
    init_uvc_camera_device(&cam1);
    
    start_uvc_camera_device(&cam1);
    
    //opengl cfg
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    //window1
    glutInitWindowPosition(50, 50);
    glutInitWindowSize(CAM1_PIXEL_WIDTH, CAM1_PIXEL_HEIGHT);
    glWindow1=glutCreateWindow("OpenGL glShowRGB_CAM1");
    glutSetWindow(glWindow1);
    glutIdleFunc(idle_global); 

    // main loop
    glutMainLoop();

    free_uvc_camera_device(&cam1);
}